﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{    
    public class Frame : VisibleGameEntity
    {
        bool _IsAppear = false;

        public bool IsAppear
        {
            get { return _IsAppear; }
            set { _IsAppear = value; }
        }
        bool _IsMoving = false;

        public Boolean IsMoving
        {
            get { return _IsMoving; }
            set { _IsMoving = value; }
        }

        
        private Vector2 _InitTopleft;

        public Vector2 InitTopleft
        {
          get { return _InitTopleft; }
          set { _InitTopleft = value; }
        }

        
        private Vector2 _CurrentTopleft;

        public Vector2 CurrentTopleft
        {
          get { return _CurrentTopleft; }
          set { _CurrentTopleft = value; }
        }

        

        private Vector2 _Direction;

        public Vector2 Direction
        {
            get { return _Direction; }
            set { _Direction = value; }
        }



        private TimeSpan _MoveTime = TimeSpan.FromSeconds(0.7);

        public TimeSpan MoveTime
        {
            get { return _MoveTime; }
            set 
            {
                _MoveTime = value;
                double movespeed_x = (float)(TopLeft.X - InitTopleft.X) / _MoveTime.TotalMilliseconds;
                double movespeed_y = (float)(TopLeft.Y - InitTopleft.Y) / _MoveTime.TotalMilliseconds;
                _MoveSpeed = new Vector2((float)movespeed_x, (float)movespeed_y);
            }
        }

        Vector2 flag = new Vector2(0, 0);
        
        
        private Vector2 _MoveSpeed;

        public event EventHandler Exited;
        public void Exit()
        {
            if (Exited != null)
                Exited(this, EventArgs.Empty);
        }

        public event EventHandler Entered;
        public void Enter()
        {
            if (Entered != null)
                Entered(this, EventArgs.Empty);
        }
        public Frame(ContentManager content, string strTexture, Vector2 topleft, Vector2 inittopleft, Vector2 size)
        {
            _TopLeft = topleft;
            _InitTopleft = inittopleft;
            _CurrentTopleft = _InitTopleft;
            Size = size;

            sprites = new List<My2Dsprite>();
            Texture2D[] textures = new Texture2D[1];
            textures[0] = content.Load<Texture2D>(strTexture);

            My2Dsprite temp = new My2Dsprite(textures, _CurrentTopleft);
            temp.Size = size;
            sprites.Add(temp);
            NSprite = sprites.Count;


            double movespeed_x = (float)(TopLeft.X - InitTopleft.X) / _MoveTime.TotalMilliseconds;
            double movespeed_y = (float)(TopLeft.Y - InitTopleft.Y) / _MoveTime.TotalMilliseconds;
            _MoveSpeed = new Vector2((float)movespeed_x, (float)movespeed_y);

            // thu thuat de khi xuat hien lan dau tien cung phai changedirection
            ChangeDirection();
            
        }

        

        public void ChangeDirection()
        {
            Vector2 temp = TopLeft;
            TopLeft = InitTopleft;
            InitTopleft = temp;
            _MoveSpeed = -_MoveSpeed;
            flag = new Vector2(0,0);
        }
        public bool MoveOnLine(GameTime gameTime, TimeSpan movetime)
        {
            
            float  deltaX, deltaY;
            if (movetime == TimeSpan.Zero)
            {
                deltaX = (float)(TopLeft.X - InitTopleft.X);
                deltaY = (float)(TopLeft.Y - InitTopleft.Y);
            }
            else
            {
                deltaX = _MoveSpeed.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                deltaY =_MoveSpeed.Y * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            Vector2 temp = CurrentTopleft;
            temp.X += deltaX;
            temp.Y += deltaY;
            
            if (_MoveSpeed.X >= 0)
            {
                if(temp.X >= TopLeft.X && flag.X != 1)
                {
                    temp.X = TopLeft.X;
                    flag.X = 1;
                }                
            }
            else
            {
                if (temp.X <= TopLeft.X && flag.X != 1)
                {
                    temp.X = TopLeft.X;
                    flag.X = 1;
                } 
            }

            if (_MoveSpeed.Y >= 0)
            {
                if (temp.Y >= TopLeft.Y && flag.Y != 1)
                {
                    temp.Y = TopLeft.Y;
                    flag.Y = 1;
                }
            }
            else
            {
                if (temp.Y <= TopLeft.Y && flag.Y != 1)
                {
                    temp.Y = TopLeft.Y;
                    flag.Y = 1;
                }
            }
            CurrentTopleft = temp;
            sprites[0].Position = temp;
            if (flag.X != 0 && flag.Y != 0)
                return false;
            return true;
            
        }
        public override void Update(GameTime gameTime, InputSystem input)
        {
            if (_IsMoving)
            {               
                if (!MoveOnLine(gameTime, _MoveTime))
                {
                    IsMoving = false;
                    if (IsAppear)
                    {
                        IsAppear = false;
                        Exit();
                    }
                    else
                    {
                        IsAppear = true;
                        Enter();
                    }
                }                               
            }
            
        }

        public void Move()
        {
            ChangeDirection();
            IsMoving = true;
        }
    }
}
